//
//  HomeStressViewModel.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/2.
//

import UIKit
import RxCocoa
import RxSwift
import MYModule

open class HomeStressViewModel: HomeBaseViewModel {
    
    public var date: Date
    
    public let UIReplay: BehaviorRelay<HomeStressModel>
    
    public required init(date: Date) {
        
        self.date = date
        
        let model = HomeStressModel()
        
        UIReplay = BehaviorRelay<HomeStressModel>(value: model)
        
        super.init(cellType: .stress)
        
        reloadData(date)
       
        
        
    }
    
    // 查询单次测量stress
    public func aStress() -> SingleStressModel? {
        
        guard let stressModel = SingleStressModel.last(from: Date().timeIntervalSince1970) else {
            return nil
        }
        
        return stressModel
        
    }
    
    public override func reloadData(_ date: Date? = nil) {
        
        let model = reloadMeasureState()
        
        guard let singleStressModel = aStress() else {
            let newModel = HomeStressModel()
            newModel.measureState = model.measureState
            UIReplay.accept(newModel)
            return
        }
        let date = Date(timeIntervalSince1970: singleStressModel.end)
        self.date = date
        
        model.date = Self.dateStr(date: date)
        model.stress = singleStressModel.stress
        
        UIReplay.accept(model)
    }
    
    @discardableResult
    public func reloadMeasureState() ->HomeStressModel {
      
        let model = UIReplay.value
        
        if BluetoothManager.shared.deviceState == .connected {
            
            if BluetoothManager.shared.measuring == .stress {
                model.measureState = .selected
            } else {
                model.measureState = (BluetoothManager.shared.measuring != .none || BluetoothManager.shared.isBatterycharging) ? .disabled : .normal
            }
            
        } else {
            model.measureState = .disabled
        }
        
        return model
    }
    
    
    public func automaticMeasure() {
        let model = UIReplay.value
        if model.measureState == .disabled {
            return
        }
        if model.measureState == .selected {
            measured()
        } else {
            measure()
        }
        
    }
    
    // 开始测量
    public func measure() {
        
        let model = UIReplay.value
        
        BluetoothManager.shared.setStartStress()
        
        model.measureState = .selected
        
        print("已经在测量了")
        
        
    }
    
    // 结束测量
    public func measured() {
        
        
        if BluetoothManager.shared.measuring == .stress {
            BluetoothManager.shared.setStopStress()
        }

        measuredData()
        print("已经结束测量了")
    }
    
    public func measuredData() {
        
        let model = UIReplay.value
        model.measureState = .normal
        reloadData(self.date)
        
        
    }
    
    
    
    
    
    
    

    
    
}
